package com.softserveinc.moviedbapp.map;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.softserveinc.moviedbapp.R;
import com.softserveinc.moviedbapp.customview.MovieTheaterBookingActivity;
import com.softserveinc.moviedbapp.util.MovieAppConstants;

import java.util.ArrayList;
import java.util.List;

public class BalloonItemizedOverlay extends ItemizedOverlay<OverlayItem> {

	private MapView mMapView;
	private List<OverlayItem> mBalloons = new ArrayList<OverlayItem>();
	private Context mContext;
	private BalloonOverlayView balloonView;
	private View clickRegion;
	private int viewOffset;
	private MapController mMapController;
	private OverlayItem currentFocussedItem;
	private int currentFocussedIndex;

	private static final String TAG = BalloonItemizedOverlay.class.getSimpleName();

	public BalloonItemizedOverlay( final Drawable defaultMarker ) {
		super( boundCenterBottom( defaultMarker ) );
	}

	/* custom constructor */
	public BalloonItemizedOverlay( final Drawable defaultMarker, final MapView mapView ) {
		this( defaultMarker );
		mContext = mapView.getContext();
		mMapView = mapView;
		mMapController = mapView.getController();
		viewOffset = 0;
	}

	@Override
	protected OverlayItem createItem( final int i ) {
		return mBalloons.get( i );
	}

	@Override
	public int size() {
		return mBalloons.size();
	}

	/* add an overlay item to the list */
	public void addOverlay( final OverlayItem item ) {
		mBalloons.add( item );
		populate();
	}

	/* @Override protected boolean onTap(final int index) { OverlayItem item = mBalloons.get(index);
	 *
	 * Log.d(TAG, item.getTitle() + "   " + item.getSnippet()); new
	 * AlertDialog.Builder(mContext).setTitle(item.getTitle()).setMessage(item.getSnippet()).create().show();
	 *
	 * return true; } */

	@Override
	protected final boolean onTap( final int index ) {

		currentFocussedIndex = index;
		currentFocussedItem = createItem( index );

		createAndDisplayBalloonOverlay();

		Log.d( TAG, currentFocussedItem.getTitle() + "   " + currentFocussedItem.getSnippet() );
		mMapController.animateTo( currentFocussedItem.getPoint() );

		return true;
	}

	protected BalloonOverlayView createBalloonOverlayView() {
		return new BalloonOverlayView( mContext, getBalloonBottomOffset() );
	}

	/**
	 * Creates and displays the balloon overlay by recycling the current balloon or by inflating it from xml.
	 *
	 * @return true if the balloon was recycled false otherwise
	 */
	private boolean createAndDisplayBalloonOverlay() {
		boolean isRecycled;
		if ( balloonView == null ) {
			balloonView = createBalloonOverlayView();
			clickRegion = balloonView.findViewById( R.id.balloon_main_layout );

			clickRegion.setOnClickListener( new OnClickListener() {

				@Override
				public void onClick( final View view ) {

					// call overridden method
					onBalloonTap( currentFocussedIndex, currentFocussedItem );

				}
			} );

			isRecycled = false;
		} else {
			isRecycled = true;
		}

		balloonView.setVisibility( View.GONE );

		List<Overlay> mapOverlays = mMapView.getOverlays();
		if ( mapOverlays.size() > 1 ) {
			hideOtherBalloons( mapOverlays );
		}

		if ( currentFocussedItem != null ) {
			balloonView.setData( currentFocussedItem );
		}

		GeoPoint point = currentFocussedItem.getPoint();
		MapView.LayoutParams params = new MapView.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
				point, MapView.LayoutParams.BOTTOM_CENTER );
		params.mode = MapView.LayoutParams.MODE_MAP;

		balloonView.setVisibility( View.VISIBLE );

		if ( isRecycled ) {
			balloonView.setLayoutParams( params );
		} else {
			mMapView.addView( balloonView, params );
		}

		return isRecycled;
	}

	public void setBalloonBottomOffset( final int pixels ) {
		viewOffset = pixels;
	}

	public int getBalloonBottomOffset() {
		return viewOffset;
	}

	protected void hideBalloon() {
		if ( balloonView != null ) {
			balloonView.setVisibility( View.GONE );
		}
	}

	private void hideOtherBalloons( final List<Overlay> overlays ) {

		for ( Overlay overlay : overlays ) {
			if ( overlay instanceof BalloonItemizedOverlay && overlay != this ) {
				( (BalloonItemizedOverlay) overlay ).hideBalloon();
			}
		}

	}

	@Override
	public OverlayItem getFocus() {
		return currentFocussedItem;
	}

	@Override
	public void setFocus( final OverlayItem item ) {
		currentFocussedItem = item;

		if ( currentFocussedItem == null ) {
			hideBalloon();
		} else {
			createAndDisplayBalloonOverlay();
		}
	}

	protected boolean onBalloonTap( final int index, final OverlayItem item ) {
		Intent movieTheaterIntent = new Intent( mContext, MovieTheaterBookingActivity.class );
		movieTheaterIntent.putExtra( MovieAppConstants.CINEMA_ID, index );
		mContext.startActivity( movieTheaterIntent );
		/* Toast.makeText(mContext, "onBalloonTap for overlay index " + index, Toast.LENGTH_LONG).show(); */
		return true;
	}

}
